home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 59 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: yarrow.wt.com.au!usenet
  2. From: bvarley@yarrow.wt.com.au (bruce varley)
  3. Newsgroups: comp.lang.c
  4. Subject: Reclaiming memory allocated recursively
  5. Date: Tue, 02 Jan 1996 02:52:35 GMT
  6. Organization: Winthrop Technology
  7. Message-ID: <4c8f8q$ln9@yarrow.wt.com.au>
  8. NNTP-Posting-Host: yarrow
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I'm using self-referential structures as described in K&R 6.5 (page
  12. 130 in my version). The program runs 'forever',  storing data in a
  13. binary tree - a days worh of data is collected, then dumped, and the
  14. whole process starts all over again. My question is:  How can I
  15. reclaim the memory that has been recursively allocated?  Using the
  16. same approach as creating the tree  - ie..........
  17.  
  18.     struct node *freetree (struct node *p)  
  19.             {
  20.             if (p != NULL)  
  21.                 {
  22.                 freetree (p -> left)   ;
  23.                 free (p -> data_pointer) ;
  24.                 freetree (p -> right)  ;
  25.                 }
  26.             return (0)  ;
  27.             }
  28.  
  29. seems unlikely to work, because the memory required to navigate
  30. through the tree is being deallocated as the process proceeds. In
  31. fact, the system crashes when I run the routine.
  32.  
  33. Any assistance would be most appreciated.
  34.  
  35. bvarley@yarrow.wt.com.au    
  36.  
  37.